package leetcode;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0541.java
 * @Author : WangRuoyu
 * @Date 2023/11/8 22:52
 */


public class Q0541 {
    public String reverseStr(String s, int k) {
        char[] ch = s.toCharArray();
        int start = 0;
        boolean flag = true;
        while (start + k < ch.length) {
            if (flag) {
                for (int i = start, j = start + k - 1; i < j; i++, j--) {
                    char tmp = ch[i];
                    ch[i] = ch[j];
                    ch[j] = tmp;
                }
            }
            start += k;
            flag = !flag;
        }
        if (flag) {
            for (int i = start, j = ch.length - 1; i < j; i++, j--) {
                char tmp = ch[i];
                ch[i] = ch[j];
                ch[j] = tmp;
            }
        }
        return String.valueOf(ch);
    }

    public static void main(String[] args) {
        Q0541 sl = new Q0541();
        String s = "abcdefg";
        int k = 2;
        System.out.println(sl.reverseStr(s, k));
    }
}